//
//  HomeFirstPageHeaderView.m
//  JiaoX
//
//  Created by yinqy on 15/1/30.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "HomeFirstPageHeaderView.h"
#import "SGFocusImageItem.h"
#import "HomeBanner.h"

@interface HomeFirstPageHeaderView()<SGFocusImageFrameDelegate>{
    
}
@end
@implementation HomeFirstPageHeaderView


-(instancetype)initWithHomeFirstPageInfo:(HomeFirstPageInfo *)pageInfo
{
    self = [super init];
    if (self) {
        [self setBackgroundColor:JXColor(238, 238, 238)];
        //设置顶部banner
        [self setTopBannerData:pageInfo.topBanner];
        [self addSubview:_bannerView];
        
        CGFloat maxY = CGRectGetMaxY(_bannerView.frame);
        
        if (pageInfo.fourBanner!=nil && pageInfo.fourBanner.list!=nil && pageInfo.fourBanner.list.count>0) {
            //设置section1
            _sectionOne = [[HomeSectionOne alloc]initWithHomeBannerList:pageInfo.fourBanner];
            _sectionOne.y = maxY+JXPublicInset;
            [self addSubview:_sectionOne];
            
            maxY = CGRectGetMaxY(_sectionOne.frame);
        }
        
        if (pageInfo.threeBanner!=nil && pageInfo.threeBanner.list!=nil && pageInfo.threeBanner.list.count>0) {
            _sectionTwo = [[HomeSectionTwo alloc]initWithHomeBannerList:pageInfo.threeBanner];
            _sectionTwo.y =maxY+JXPublicInset;
            [self addSubview:_sectionTwo];
            
            maxY = CGRectGetMaxY(_sectionTwo.frame);
        }
        
        if (pageInfo.twoBanner!=nil && pageInfo.twoBanner.list!=nil && pageInfo.twoBanner.list.count>0) {
            _sectionThree = [[HomeSectionThree alloc]initWithHomeBannerList:pageInfo.twoBanner];
            _sectionThree.y = maxY+JXPublicInset;
            [self addSubview:_sectionThree];
            maxY = CGRectGetMaxY(_sectionThree.frame);
        }
        
        if (pageInfo.productList!=nil && pageInfo.productList.list!=nil && pageInfo.productList.list.count>0) {
            [pageInfo.productList.list makeObjectsPerformSelector:@selector(setProdPromotion:) withObject:nil];
            _sectionFour = [[HomeSectionFour alloc]initWithHomeProdList:pageInfo.productList];
            _sectionFour.y = maxY +JXPublicInset;
            [self addSubview:_sectionFour];
            maxY = CGRectGetMaxY(_sectionFour.frame);
        }
        
        [self setSize:CGSizeMake(JXScreenW, maxY)];
    }
    return self;
}


-(void)setTopBannerData:(HomeBannerList *)bannerList{
    if(!bannerList.list.count)return;
    
    NSMutableArray *itemArray = [NSMutableArray arrayWithCapacity:bannerList.list.count+2];
    
    for (int i = 0; i < bannerList.list.count; i++)
    {
        HomeBanner *banner = bannerList.list[i];
        SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithTitle:banner.url image:banner.bannerImg tag:i];
        [itemArray addObject:item];
    }
    
    if(bannerList.list.count>1){
        HomeBanner *startBanner = [bannerList.list lastObject];
        SGFocusImageItem *startItem = [[SGFocusImageItem alloc] initWithTitle:startBanner.url image:startBanner.bannerImg tag:bannerList.list.count-1];
        [itemArray insertObject:startItem atIndex:0];
        
        HomeBanner *endBanner = [bannerList.list firstObject];
        SGFocusImageItem *endItem = [[SGFocusImageItem alloc] initWithTitle:endBanner.url image:endBanner.bannerImg tag:bannerList.list.count-1];
        [itemArray addObject:endItem];
    }
    SGFocusImageFrame *bannerView = [[SGFocusImageFrame alloc] initWithFrame:CGRectMake(0, 0, JXScreenW, JXScreenW*0.5) delegate:self imageItems:itemArray isAuto:YES];
    [bannerView setBackgroundColor:JXGlobalColorBg];                                                                                                                     
    [bannerView scrollToIndex:0];
    _bannerView = bannerView;
}

- (void)sectionOneClick:(NSString *)urlStr{
    if([self.delegate respondsToSelector:@selector(sectionOneClick:)]){
        [self.delegate sectionOneClick:urlStr];
    }
}
- (void)sectionTwoClick:(NSString *)urlStr{
    if([self.delegate respondsToSelector:@selector(sectionTwoClick:)]){
        [self.delegate sectionTwoClick:urlStr];
    }
}
- (void)sectionThreeClick:(NSString *)urlStr{
    if([self.delegate respondsToSelector:@selector(sectionThreeClick:)]){
        [self.delegate sectionThreeClick:urlStr];
    }
}
- (void)sectionFourClick:(ProductItemInfo *)prodItem{
    if([self.delegate respondsToSelector:@selector(sectionFourClick:)]){
        [self.delegate sectionFourClick:prodItem];
    }
}

@end
